package mobile.android.portal.ui;

import java.net.ConnectException;

import mobile.android.portal.R;
import mobile.android.portal.obj.User;
import mobile.android.portal.util.Session;
import mobile.android.portal.webservice.BusinessPortalWebService;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity{

	
	private EditText  login_username;
	private EditText login_password;
	Handler loginHandler;
	User user;
	public int resultCount;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        
        login_username = (EditText) findViewById(R.id.login_username);
        login_password = (EditText) findViewById(R.id.login_password);
        
        
    }
    
    public void onLogInButtonClick(View v) {
    	Intent logInSceen = new Intent(this, MobileBusinessPortalActivity.class);
    	
    	if(login_username.getText().toString().length()!= 0 && login_password.getText().toString().length()!= 0){
    		
    		new WSDOLogin().execute(null,null,null);
    		
    		loginHandler=new Handler(){
    			public void handleMessage(Message message) {
    				/*
    				 *  If the response message is not an empty message display the output. Otherwise display 
    				 * alert dialog which contains the error message.
    				 *  */
    				if (message.what != -1) {
    					resultCount = user.getCount();
    					
    					
    					
    					/*
    					 *  If no information available set GUI components invisible and display information 
    					 * not available toast message. Otherwise display the post office information.
    					 *  */
    					if (user.getCount() <= 0) {
    						Toast noInfomationToastMesage = Toast.makeText(LoginActivity.this, "Authentication Failed ..try again ", Toast.LENGTH_LONG);
    						noInfomationToastMesage.show();
    					} else {    						
							Session.setUser(user);
				    		if(Session.getUser()!=null){
				    			//Log.v("Session user", Session.getUser().getProducer_name());
				    		}
    						Toast.makeText(getApplicationContext(),	"Successfully Logged in ! ", Toast.LENGTH_SHORT).show();
    					}
    					
    				}
    			}
    		};
    		   		
    		
    		startActivity(logInSceen);   		
    		
    	}else{
    		Toast.makeText(getApplicationContext(),	"All fields are required.. please fill ! ", Toast.LENGTH_SHORT).show();
    	}
    	
    	
    }
    
    
    class WSDOLogin extends AsyncTask<Object, Object, Object> {

		@Override
		protected Object doInBackground(Object... params) {
			try {
				Log.v("calling - ", "WSDOLogin");
				user = BusinessPortalWebService.doLogin(login_username.getText().toString(), login_password.getText().toString());
				loginHandler.sendMessage(loginHandler.obtainMessage());
				return null;

			} catch (ConnectException e) {
				/*
				 * Sends an empty message to the web service client if an
				 * ConnectException occurred
				 */
				Log.i("ConnectException", "ConnectException occured");
				e.printStackTrace();

			} catch (Exception e) {
				// TODO Auto-generated catch block
				// Send an empty message if an exception occurred
				Log.i("Exception", "Exception occured");
				e.printStackTrace();
			}
			return null;
		}
	}
    
}
